package org.csu.pet_store_back.web;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.csu.pet_store_back.common.CommonResponse;
import org.csu.pet_store_back.domain.Account;
import org.csu.pet_store_back.service.AccountService;
import org.csu.pet_store_back.service.Impl.AccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@Validated
public class AccountController {
    @Resource
    private AccountService accountService;

    /**
     * 用户登录
     **/
    @GetMapping("/users")
    public CommonResponse<Account> login(
            @RequestParam("userid") String userid,
            @RequestParam("password") String password,
            HttpSession session
    ){
        System.out.println(userid + password);
        CommonResponse<Account> result = accountService.login(userid,password);
        if (result.isSuccess()){
            session.setAttribute("loginAccount",result.getData());
        }
        return result;
    }

    /**
     * 用户注册
     **/
    @PostMapping("/users")
    public CommonResponse<Account> register(
            @RequestBody Account account,
            HttpSession session
    ){
        System.out.println(account);
        CommonResponse<Account> result = accountService.register(account);
        if (result.isSuccess()){
            session.setAttribute("registerAccount",result.getData());
        }
        return result;
    }

    /**
     * 用户修改
     **/
    @PutMapping("/users")
    public CommonResponse<Account> updates(
            @RequestBody Account account,
            HttpSession session
    ){
        CommonResponse<Account> result = accountService.update(account);
        if (result.isSuccess()){
            session.setAttribute("updateAccount",result.getData());
        }
        return result;
    }

    /**
     * Linux测试请求
     * */
    @GetMapping("/tests")
    public String test(){
        return "{\"message\":\"Hello,TEST!\"}";
    }
}
